<nz-spin *ngIf="!repository" nzTip="Loading repository"></nz-spin>

<ng-container *ngIf="repository && vcsProject">
    <div class="content">

        <nz-alert nzType="info" [nzDescription]="infoMsg" nzShowIcon>
            <ng-template #infoMsg>
                Make sure that user <b>{{vcsProject.auth.username}}</b> can read the target repository.
            </ng-template>
        </nz-alert>

        <nz-descriptions nzTitle="Repository info" [nzColumn]="2">
            <nz-descriptions-item nzTitle="ID">{{repository.id}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Name">{{repository.name}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Clone URL">{{repository.clone_url}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Added">{{repository.created}} by
                {{repository.created_by}}</nz-descriptions-item>
        </nz-descriptions>

        <div class="events">
            <h3>Repository events <i (click)="loadHookEvents()" nz-icon nzType="sync" [nzSpin]="loadingHooks"
                    nzTheme="outline" class="pointing"></i> </h3>
            <nz-table [nsAutoHeightTable]="85" [nzLoading]="loadingHooks" [nzData]="hookEvents"
                [nzFrontPagination]="false" nzSize="small" #hooksTable>
                <thead>
                    <tr>
                        <th>Status</th>
                        <th [nzSortFn]="sortHookByDate" nzSortOrder="descend">Date</th>
                        <th>ID</th>
                        <th [nzFilterMultiple]="true" [nzFilters]="eventFilterList" [nzFilterFn]="eventFilterFunc">Event
                        </th>
                        <th>Ref</th>
                        <th>CDS User</th>
                        <th>Analysis</th>
                        <th>Triggered Workflows</th>
                        <th>Error</th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let h of hooksTable.data" (click)="displayDetail(h)" class="pointing">
                        <td>
                            <app-status-icon [status]="h.status" nz-tooltip="{{h.last_error}}"></app-status-icon>
                        </td>
                        <td><app-date-from-now [value]="h.created_string"></app-date-from-now></td>
                        <td>{{h.uuid}}</td>
                        <td>{{h.event_name}}{{h.event_type ? ' (' + h.event_type + ')' : ''}}</td>
                        <td>{{h?.extracted_data?.ref}}</td>
                        <td>{{h.username}}</td>
                        <td>
                            <ng-container *ngFor="let a of h?.analyses">
                                <app-status-icon *ngIf="a.project_key === project.key"
                                    [status]="a.status"></app-status-icon>
                            </ng-container>
                        </td>
                        <td>
                            <ng-container
                                [ngSwitch]="h.nbFailed > 0? 'fail': (h.nbScheduled > 0? 'scheduled': (h.nbDone >0? 'success': 'skipped'))">
                                <ng-container *ngSwitchCase="'fail'">
                                    <app-status-icon [status]="'Error'"></app-status-icon>
                                </ng-container>
                                <ng-container *ngSwitchCase="'success'">
                                    <app-status-icon [status]="'Success'"></app-status-icon>
                                </ng-container>
                                <ng-container *ngSwitchCase="'scheduled'">
                                    <app-status-icon [status]="'Scheduled'"></app-status-icon>
                                </ng-container>
                                <ng-container *ngSwitchCase="'skipped'">
                                    <app-status-icon [status]="'Skipped'"></app-status-icon>
                                </ng-container>
                            </ng-container>
                        </td>
                        <td>
                            {{h.last_error}}
                        </td>
                    </tr>
                </tbody>
            </nz-table>
        </div>
    </div>
    <div class="footer">
        <div class="buttons">
            <button nz-button nzType="default" [nzLoading]="loading" (click)="openTriggerAnalysisDrawer()">
                Trigger analysis
            </button>
            <button nz-button nzDanger nzType="primary" [nzLoading]="loading" nz-popconfirm
                nzPopconfirmTitle="Are you sure you want to remove this repository ?"
                (nzOnConfirm)="removeRepositoryFromProject()" nzPopconfirmPlacement="topRight">
                Remove
            </button>
        </div>
    </div>
</ng-container>

<nz-modal *ngIf="selectedHookEvent" nzVisible nzTitle="Event details" nzCentered nzWidth="1200px"
    (nzOnCancel)="closeModal()" [nzFooter]="null">
    <div class="modal-content" *nzModalContent>
        <nz-descriptions nzTitle="Event" [nzColumn]="2" nzBordered>
            <nz-descriptions-item nzTitle="ID"><span nz-typography nzCopyable
                    [nzContent]="selectedHookEvent.uuid"></span></nz-descriptions-item>
            <nz-descriptions-item nzTitle="Status">{{selectedHookEvent.status}}</nz-descriptions-item>
            <nz-descriptions-item *ngIf="selectedHookEvent.last_error"
                nzTitle="Error">{{selectedHookEvent.last_error}}</nz-descriptions-item>
            <nz-descriptions-item
                nzTitle="Event">{{selectedHookEvent.extracted_data.cds_event_name}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Event type"
                *ngIf="selectedHookEvent.extracted_data.cds_event_type">{{selectedHookEvent.extracted_data.cds_event_type}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Ref">{{selectedHookEvent.extracted_data.ref}}</nz-descriptions-item>
            <nz-descriptions-item nzTitle="Commit"><span nz-typography nzCopyable
                    [nzContent]="selectedHookEvent.extracted_data.commit"></span></nz-descriptions-item>
            <nz-descriptions-item nzTitle="Username">{{selectedHookEvent.username}}</nz-descriptions-item>
            <nz-descriptions-item *ngIf="selectedHookEvent.sign_key"
                nzTitle="PGP Key">{{selectedHookEvent.sign_key}}</nz-descriptions-item>
        </nz-descriptions>

        <nz-descriptions nzTitle="Analysis" [nzColumn]="2" [nzBordered]="selectedAnalysis">
            <ng-container *ngIf="selectedAnalysis">
                <nz-descriptions-item nzTitle="ID"><span nz-typography nzCopyable
                        [nzContent]="selectedAnalysis.id"></span></nz-descriptions-item>
                <nz-descriptions-item nzTitle="Date">{{selectedAnalysis.last_modified}}</nz-descriptions-item>
                <nz-descriptions-item nzTitle="Status">{{selectedAnalysis.status}}</nz-descriptions-item>
                <nz-descriptions-item nzTitle="Username">{{selectedAnalysis.data.cds_username}}</nz-descriptions-item>
                <nz-descriptions-item *ngIf="selectedAnalysis.data.error"
                    nzTitle="Error">{{selectedAnalysis.data.error}}</nz-descriptions-item>
                <nz-descriptions-item *ngIf="selectedAnalysis.data.operation_uuid" nzTitle="Operation ID"><span
                        nz-typography nzCopyable
                        [nzContent]="selectedAnalysis.data.operation_uuid"></span></nz-descriptions-item>
                <nz-descriptions-item *ngIf="selectedAnalysis.data.sign_key_id"
                    nzTitle="PGP Key">{{selectedAnalysis.data.sign_key_id}}</nz-descriptions-item>
            </ng-container>
        </nz-descriptions>
        <ng-container *ngIf="!selectedAnalysis">
            <nz-alert [nzMessage]="'No analysis'" nzType="info"></nz-alert>
        </ng-container>

        <nz-descriptions nzTitle="Entities" [nzColumn]="2" [nzBordered]="selectedAnalysisEntities">
            <ng-container *ngFor="let entry of selectedAnalysisEntities | keyvalue">
                <nz-descriptions-item *ngIf="entry.value.success.nb > 0"
                    [nzTitle]="entry.key + ' updated (' + entry.value.success.nb + ')'">
                    <span *ngFor="let f of entry.value.success.files">{{f}}<br /></span>
                </nz-descriptions-item>
                <nz-descriptions-item *ngIf="entry.value.skipped.nb > 0"
                    [nzTitle]="entry.key + ' skipped (' + entry.value.skipped.nb + ')'">
                    <span *ngFor="let f of entry.value.skipped.files">{{f}}<br /></span>
                </nz-descriptions-item>
            </ng-container>
        </nz-descriptions>
        <ng-container *ngIf="!selectedAnalysisEntities">
            <nz-alert nzMessage="No entities found" nzType="info"></nz-alert>
        </ng-container>

        <div class="workflows">
            <h3>Workflows</h3>
            <nz-list nzBordered nzSize="small"
                [nzBordered]="selectedHookEvent.workflows && selectedHookEvent.workflows.length > 0">
                <nz-list-item *ngFor="let w of selectedHookEvent.workflows">
                    <ng-container *ngIf="w.status === 'Done'">
                        <app-status-icon [status]="'Success'" title="Done"></app-status-icon>
                        <a
                            [routerLink]="['/project', w.project_key, 'run', w.run_id]">{{w.project_key}}/{{w.workflow_name}}#{{w.run_number}}</a>
                    </ng-container>
                    <ng-container *ngIf="w.status !== 'Done'">
                        <app-status-icon [status]="w.status" [title]="w.status"></app-status-icon>
                        <span>{{w.project_key}}/{{w.workflow_name}}{{w.error ? ' - Error: ' + w.error : ''}}</span>
                    </ng-container>
                </nz-list-item>
            </nz-list>
        </div>
        <ng-container *ngIf="!selectedHookEvent.workflows || selectedHookEvent.workflows.length === 0">
            <nz-alert [nzMessage]="'No workflow has been triggered'" nzType="info"></nz-alert>
        </ng-container>
    </div>
</nz-modal>